﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class TextureComposeWin : EditorWindow
{
    [MenuItem("GStore/贴图工具/贴图通道合并工具")]
    static void GetWindow()
    {
        GetWindow(typeof(TextureComposeWin), false, "贴图通道合并工具");
    }


    private Texture2D tex_R, tex_G, tex_B, tex_A;
    void OnGUI()
    {
        EditorGUILayout.BeginVertical("box");

        tex_R =(Texture2D)EditorGUILayout.ObjectField("R通道",tex_R, typeof(Texture2D),false);
        tex_G = (Texture2D)EditorGUILayout.ObjectField("G通道",tex_G, typeof(Texture2D), false);
        tex_B = (Texture2D)EditorGUILayout.ObjectField("B通道",tex_B, typeof(Texture2D), false);
        tex_A = (Texture2D)EditorGUILayout.ObjectField("A通道",tex_A, typeof(Texture2D), false);

        
        if (GUILayout.Button("合并贴图"))
        {
            var path = EditorUtility.SaveFilePanelInProject("文件保存", "NewComposeTexutre", "png", "保存文件中");

            TextureCompose texCompose = new TextureCompose();
            texCompose.SetTexture(TextureCompose.Channel.R, tex_R);
            texCompose.SetTexture(TextureCompose.Channel.G, tex_G);
            texCompose.SetTexture(TextureCompose.Channel.B, tex_B);
            texCompose.SetTexture(TextureCompose.Channel.A, tex_A);

            texCompose.ComposeTexture();
            texCompose.SaveTexture(path);
        }
        EditorGUILayout.EndVertical();
    }

    private Texture2D GetChannelPreviewTexture(TextureCompose.Channel channel,Texture2D tex)
    {
        Texture2D maskTex = new Texture2D(tex.width,tex.height);
        Color[] colors = new Color[tex.width * tex.height];

        ForceTextureReadable(tex);

        var srcColors = maskTex.GetPixels();

        for (int i = 0; i < colors.Length; i++)
        {
           
            switch (channel)
            {
                case TextureCompose.Channel.R:
                    colors[i] = new Color(srcColors[i].r, srcColors[i].r, srcColors[i].r, srcColors[i].r);
                     break;
                case TextureCompose.Channel.G:
                    colors[i] = new Color(srcColors[i].g, srcColors[i].g, srcColors[i].g, srcColors[i].g);
                    break;
                case TextureCompose.Channel.B:
                    colors[i] = new Color(srcColors[i].b, srcColors[i].b, srcColors[i].b, srcColors[i].b);
                    break;
                case TextureCompose.Channel.A:
                    colors[i] = new Color(srcColors[i].a, srcColors[i].a, srcColors[i].a, srcColors[i].a);
                    break;
            }
        }
        maskTex.SetPixels(colors);
        return maskTex;
    }

    private void ForceTextureReadable(Texture2D tex)
    {
        var path = AssetDatabase.GetAssetPath(tex);
        TextureImporter TextureI = AssetImporter.GetAtPath(path) as TextureImporter;
        TextureI.isReadable = true;
        AssetDatabase.Refresh();
        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
    }
}
